﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsAppOpenFile
{
    class PrinterUsage
    {
        public static void PrintTable(string url)
        {
            string aa = ParseFilePath(url);
            // Handle null input and split string into list
            List<string> fields = !string.IsNullOrEmpty(aa) ? aa.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries).ToList() : new List<string>();
            // 初始化8行4列表格数据
            string[,] tableData = new string[8, 4] {
            {GValues.companyName, "", "", ""},
            {"物料周转标识单", "", "", ""},
            {"客户", fields[0] + "/" + fields[1], "", ""},
            {"图号", fields[2], "", ""},
            {"名称", fields[3], "", ""},
            {"转出", fields[4] + "/" + fields[5], "", ""},
            {"接收", fields[6], "", ""},
            {"日期", fields[7], "数量", fields[8]}
        };

            var printer = new TablePrinter(tableData,fields[5] + "/" + fields[8],GValues.printName);
            // 设置打印机（可选）
            // printer.PrinterName = "Your Printer Name";

            // 直接打印
            printer.Print();

            // 或显示预览
            //printer.PrintPreview();
        }
        static string ParseFilePath(string url)
        {
            // 假设 URL 格式为 myapp://open?file=C:\Path\To\Your\File.txt
            int startIndex = url.IndexOf("print=") + 6;
            if (startIndex >= 6)
            {
                return Uri.UnescapeDataString(url.Substring(startIndex));
            }
            return null;
        }
    }
}
